﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LYQ.Model;
using LYQ.BLL;

namespace BaoJian
{
    public partial class C : System.Web.UI.Page
    {
        string oid;
        public LYQ.Model.Doc model;
        public string CurrentPos;
        private string columnID;
        string Region = (HttpContext.Current.Request.Cookies["region"] == null || HttpContext.Current.Request.Cookies["region"].Value == "" || HttpContext.Current.Request.Cookies["region"].Value.ToLower() == "beijing") ? "beijing" : HttpContext.Current.Request.Cookies["region"].Value as string;
        protected void Page_Load(object sender, EventArgs e)
        {          
            oid = HttpContext.Current.Request.QueryString["id"];
            if (Region == "beijing")   //若是北京站点
            {
                LocalHeader1.Visible = false;
                Header1.Visible = true;
            }
            else                     //若是其他站点
            {
                LocalHeader1.Visible = true;
                Header1.Visible = false;
            }

            if (oid == null)
            {
                HttpContext.Current.Response.Redirect("default.aspx");
            }

            if (!Page.IsPostBack)
            {
               try // 不知道什么问题，先catch. by wl.
               {
                  BindData();
                  GetCurrentPos();
               }
               catch { }
            }
        }

        private void GetCurrentPos()
        {
            //TODO:这里需要进一步的优化       
            //取得当前栏目的所有父栏目,生成当前位置
            string sqlSysHiber = "oid='{0}' AND (Parentid <> '19800628-0000-0000-0000-000000000001') AND (Parentid <> 'eb8b49b9-e760-497b-83cf-d9fd1d9f0833') AND (Parentid <> 'FD870D78-0DB5-48D5-AF31-4D6ACC3FA90D')";
            sqlSysHiber = string.Format(sqlSysHiber, columnID);
            DataSet hiberds = Tools.Common.GetSysobj_HiberFromCache();
            DataSet objds = Tools.Common.GetSysobjFromCache();
            DataRow[] dr = hiberds.Tables[0].Select(sqlSysHiber, "Plevcod asc");
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < dr.Length; i++)
            {
                DataRow[] d = objds.Tables[0].Select("oid='" + dr[i]["Parentid"] + "'");
                sb.Append("<a href=\"col.aspx?p=" + d[0]["OID"] as string + "\">" + d[0]["oname"] as string + "</a> > >");
            }
     

            DataRow[] t = objds.Tables[0].Select("oid='" + columnID + "'");
            sb.Append(" <a href=\"col.aspx?p=" + (t[0]["OID"] as string) + "\">" + t[0]["oname"] as string + "</a> ");

            if (Region == "beijing" || Region == null || Region == "")
            {
                sb.Insert(0, "<a href=\"default.aspx\">首页</a> > > ");

            }
            else
            {
                sb.Insert(0, "<a href=\"/" + Region + "/default.aspx\">首页</a> > > ");
            }
            CurrentPos = sb.ToString();


        }

        private void BindData()
        {
            LYQ.BLL.Doc bll = new LYQ.BLL.Doc();
            model = bll.GetModel(oid); 
            if(model == null)
            {
                HttpContext.Current.Response.Redirect("default.aspx");
            }

            columnID = model.PID;
           
            model.BrowserNumber += 1;
            bll.Update(model);
           


            ////相关栏目
            LYQ.BLL.RelatedColumn bllCol = new LYQ.BLL.RelatedColumn();
            LYQ.BLL.RelateImage bllImg = new LYQ.BLL.RelateImage();
            LYQ.Model.RelatedColumn modelCol = bllCol.GetModel("c.aspx", model.PID, Region);
            //TODO:暂时先删除
            //if (modelCol == null)
            //{
            //    HttpContext.Current.Response.Redirect("default.aspx");
            //}

            //相关图片
            LYQ.Model.RelateImage modelImg = bllImg.GetModel("c.aspx", model.PID, Region);

            //TODO:暂时先删除
            //if (modelImg == null)
            //{
            //    HttpContext.Current.Response.Redirect("default.aspx");
            //}

            colnews1.ColumnID = modelCol.AuxiColumnID1;
            colnews2.ColumnID = modelCol.AuxiColumnID2;
            colnews3.ColumnID = modelCol.AuxiColumnID3;
            colnews4.ColumnID = modelCol.AuxiColumnID4;

            string path = @"/uploads/ad/" + Region + @"/";

            ColImg1.Src = path + modelImg.ImagePath1;
            ColImg1.URL = modelImg.URL1;
            ColImg2.Src = path + modelImg.ImagePath2;
            ColImg2.URL = modelImg.URL2;
            ColImg3.Src = path + modelImg.ImagePath3;
            ColImg3.URL = modelImg.URL3;
            ColImg4.Src = path + modelImg.ImagePath4;
            ColImg4.URL = modelImg.URL4;         
       
        }
    }
}
